<?php
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 网城创想多用户商城 项目的一部分
//
// Copyright (c) 2007 - 2008 www.shopnc.net
//
// 要查看完整的版权信息和许可信息，请查看源代码中附带的 COPYRIGHT 文件，
// 或者访问 http://www.shopnc.net/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : own_recieve.php   FILE_PATH : D:\webroot\mulitishop\member\own_recieve.php
 * ....收货地址管理
 *
 * @copyright Copyright (c) 2007 - 2007 www.shopnc.net 
 * @author 网城创想多用户商城开发团队 php_netproject@yahoo.com.cn
 * @package 
 * @subpackage 
 * @version Fri Sep 21 16:25:13 CST 2007
 */

require ("../global.inc.php");

class OwnReceiveManage extends CommonFrameWork{
	/**
	 * 收货地址对象
	 *
	 * @var obj
	 */
	var $obj_receive;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $objvalidate;
	function main(){
		/**
		 * 创建文章对象
		 */
		if (!is_object($this->obj_receive)){
			require_once("receive.class.php");
			$this->obj_receive = new ReceiveClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->objvalidate)){
			require_once("commonvalidate.class.php");
			$this->objvalidate = new CommonValidate();
		}

		/**
		 * 设置模板路径
		 */
		$this->setsubtemplates("member");

		/**
		 * 语言包
		 */
		$this->getlang("article");

		$this->memberMenuModule();

		switch($this->_input['action']){
			case "addsave":
				$this->_addsaveReceive();
				break;
			case "del":
				$this->_deleReceive();
				break;
			case "modisave":
				$this->_modisaveReceive();
				break;
			default:
				$this->_getReceive();
		}
	}

	/**
	 * 得到收货地址列表
	 *
	 */
	function _getReceive(){
		$receive_array = $this->obj_receive->getReceive($_SESSION['s_login']['id']);
		//print_r($receive_array);
		$this->output('receive_array',$receive_array);
		$this->showpage("own_receive.manage");
	}

	/**
	 * 保存新增收货地址
	 *
	 */
	function _addsaveReceive(){
		$error = $this->_Validate();
		/*
		$this->objvalidate->validateparam = array(
		array("input"=>$this->_input["txtReceiveName"],"require"=>"true","message"=>"请填写接收人"),
		array("input"=>$this->_input["txtProvince"],"require"=>"true","message"=>"请选择寄送省市"),
		array("input"=>$this->_input["txtCity"],"require"=>"true","message"=>"请选择寄送市区"),
		array("input"=>$this->_input["txtAddress"],"require"=>"true","message"=>"请填写寄送地址"),
		);
		*/
		$error = $this->objvalidate->validate();
		if ($error != "" ){
			$this->redirectPath("error","",$error);
		}else{
			/**
			 * 获得随机的唯一编码
			 */
			$receive_last_id = $this->obj_receive->getReceiveLastId();
			if("" == $receive_last_id){
				$receive_last_id = 1;
			}else{
				$receive_last_id += 1;
			}
			$chars = array(
			"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
			"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
			"w", "x", "y", "z"
			);
			$random_string = Common::genRandomString($chars, 4);
			$this->_input["txtRcode"] = md5($receive_last_id.$random_string);

			$this->_input['member_id'] = $_SESSION['s_login']['id'];
			$this->obj_receive->addReceive($this->_input);
			$this->redirectPath("succ","/member/own_receive.php","您新增收货地址成功！");
		}
	}
	
	/**
	 * 删除收货地址
	 *
	 */
	function _deleReceive(){
		$this->obj_receive->delReceive($this->_input['receiveid'],$_SESSION['s_login']['id']);
		$this->redirectPath("succ","/member/own_receive.php","您删除收货地址成功！");
	}
	
	/**
	 * 保存修改信息
	 *
	 */
	function _modisaveReceive(){
		$error = $this->_Validate();
		//$error = $this->_Validate();
		/*
		$this->objvalidate->validateparam = array(
		array("input"=>$this->_input["txtReceiveName"],"require"=>"true","message"=>"请填写接收人"),
		array("input"=>$this->_input["txtProvince"],"require"=>"true","message"=>"请选择寄送省市"),
		array("input"=>$this->_input["txtCity"],"require"=>"true","message"=>"请选择寄送市区"),
		array("input"=>$this->_input["txtAddress"],"require"=>"true","message"=>"请填写寄送地址"),
		);
		*/
		$error = $this->objvalidate->validate();
		if ($error != "" ){
			//echo $error;
			$this->redirectPath("error","",$error);
		}else{
			$this->_input['member_id'] = $_SESSION['s_login']['id'];
			$exist = $this->obj_receive->isExistReceive($this->_input['receive_id'],'receive_id',$this->_input['member_id']);
			if ($exist == true){
				$this->obj_receive->modiReceive($this->_input);
				$this->redirectPath("succ","/member/own_receive.php","您修改收货地址成功！");exit;
			}
			$this->redirectPath("error","","收货地址不存在！".$exist);
		}
	}
	
	/**
	 * 验证修改新增收货地址
	 *
	 * @return unknown
	 */
	function _Validate(){
		/**
		 * 验证信息
		 */
		$this->objvalidate->setValidate(array("input"=>$this->_input['txtReceiveName'],"require"=>"true","message"=>'请填写收货人姓名'));
		$this->objvalidate->setValidate(array("input"=>$this->_input['txtProvince'],"require"=>"true","message"=>'请填写所在地区'));
		$this->objvalidate->setValidate(array("input"=>$this->_input['txtCity'],"require"=>"true","message"=>'请填写所在地区'));
		$this->objvalidate->setValidate(array("input"=>$this->_input['txtAddress'],"require"=>"true","message"=>'请填写详细地址'));
		$this->objvalidate->setValidate(array("input"=>$this->_input['txtZip'],"require"=>"true","message"=>'请填写邮政编码'));
		$error = $this->objvalidate->validate();
		return $error;
	}
}

$receive_manage = new OwnReceiveManage();
$receive_manage->main();
unset($receive_manage);
?>